/* ---------------------------------------------------------------------------------------------
 *
 *            Capital Alliance Software Confidential Proprietary 
 *            (c) Copyright CAS 201{x}, All Rights Reserved
 *                          www.pekall.com
 *
 * ----------------------------------------------------------------------------------------------
 */

package com.android.music.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;

public class SQLHelper extends SQLiteOpenHelper {

    public SQLHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        open(context);
    }

    public static String[] createTableSQL = {
            "CREATE TABLE IF NOT EXISTS  t_music_history ("
                    + "_id INTEGER PRIMARY KEY AUTOINCREMENT,_path TEXT,_duration TEXT,_description TEXT);",
            "CREATE TABLE IF NOT EXISTS t_my_play_list (_id INTEGER PRIMARY KEY AUTOINCREMENT,_title TEXT);",
            "CREATE TABLE IF NOT EXISTS my_play_list_music_map (_id INTEGER PRIMARY KEY AUTOINCREMENT,_play_list_id INTEGER,_music_path TEXT,_duration TEXT,_description TEXT);"
    };

    public static String[] tableName = {
            "t_music_history", "t_my_play_list", "my_play_list_music_map"
    };

    public static String insertSQL = " INSERT INTO t_my_play_list values (,'我的最爱');";

    public final static String DB_NAME = "pekallmusic.db";

    public final static int DB_VERSION = 1;

    public static final String AUTHORITY = "com.android.music.provider";

    public static final int MUSIC_HISTORY_INDEX = 1;

    public static final int MUSIC_HISTORY_ID_INDEX = 2;

    public static final int MUSIC_MY_PLAY_LIST_INDEX = 3;

    public static final int MUSIC_MY_PLAY_LIST_ID_INDEX = 4;

    public static final int MUSIC_MY_PLAY_LIST_MUSIC_MAP_INDEX = 5;

    public static final int MUSIC_MY_PLAY_LIST_MUSIC_MAP_ID_INDEX = 6;

    public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.android.music.dir";

    public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.android.music.item";

    public static final Uri CONTENT_URI_HISTORY = Uri.parse("content://" + AUTHORITY
            + "/t_music_history");

    public static final Uri CONTENT_URI_PLAY_LIST = Uri.parse("content://" + AUTHORITY
            + "/t_my_play_list");

    public static final Uri CONTENT_URI_PLAY_LIST_MUSIC_MAP = Uri.parse("content://" + AUTHORITY
            + "/my_play_list_music_map");

    private SQLiteDatabase db;

    public void onCreate(SQLiteDatabase db) {
        this.db = db;
        db.execSQL(createTableSQL[0]);
        db.execSQL(createTableSQL[1]);
        db.execSQL(createTableSQL[2]);
        // db.execSQL(insertSQL);
        ContentValues values = new ContentValues();
        values.put("_title", "我的最爱");
        db.insert(tableName[1], null, values);
    }

    public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
        db.execSQL("DROP TABLE IF EXISTS " + tableName[0]);
        db.execSQL("DROP TABLE IF EXISTS " + tableName[1]);
        db.execSQL("DROP TABLE IF EXISTS " + tableName[2]);
    }

    public void open(Context context) {
        if (null == db || !this.db.isOpen()) {
            this.db = context.openOrCreateDatabase(DB_NAME, Context.MODE_PRIVATE, null);
        }
    }
}
